package cn.liangcaomei.eduservice.controller;


import cn.liangcaomei.eduservice.entity.EduTeacher;
import cn.liangcaomei.eduservice.entity.vo.TeacherQuery;
import cn.liangcaomei.eduservice.service.EduTeacherService;
import cn.liangcaomei.commonutils.R;
import cn.liangcaomei.servicebase.exceptionhandler.CustException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author LiuJie
 * @since 2024-01-12
 */
@Api(description="讲师管理")
@RestController
@RequestMapping("/eduservice/teacher")
//解決跨域問題
@CrossOrigin
public class EduTeacherController {

    //access address : http://localhost:8001/eduservice/teacher/findAll
    //1.inject service Object
    @Autowired
    private EduTeacherService teacherService;

    //require all teacher table record
    //rest style
    /*@ApiOperation(value = "所有讲师列表")
    @GetMapping("findAll")
    public List<EduTeacher> findAllTeacher(){
        List<EduTeacher> list = teacherService.list(null);
        return list;
    }*/
    //用統一返回方式優化
    @ApiOperation(value = "所有讲师列表")
    @GetMapping("findAll")
    public R findAllTeacher(){
        List<EduTeacher> list = teacherService.list(null);
        return R.ok().data("items",list);
    }

    //2.logic delete teacher record
    /*@ApiOperation(value = "根据ID删除讲师")
    @DeleteMapping("{id}")
    public boolean removeTeacher(@ApiParam(name = "id", value = "讲师ID", required = true) @PathVariable String id){
        boolean flag = teacherService.removeById(id);
        return flag;
    }*/
    //用統一返回方式優化
    @ApiOperation(value = "根据ID删除讲师")
    @DeleteMapping("{id}")
    public R removeTeacher(@ApiParam(name = "id", value = "讲师ID", required = true) @PathVariable String id){
        boolean flag = teacherService.removeById(id);
        if (flag){
            return R.ok();
        } else {
            return R.error();
        }
    }

    //3.paging query
    //current: current page
    //limit: per page total size
    @ApiOperation(value = "分頁查詢")
    @GetMapping("pagingTeacher/{current}/{limit}")
    public R pageListTeacher(@PathVariable long current,
                             @PathVariable long limit){
        //create page Object
        Page<EduTeacher> pageTeacher = new Page<>(1,3);

        try {
            int i = 1/0;
        } catch (Exception e) {
            //執行異常處理
            throw new CustException(20001,"執行了自定義異常處理...");
        }


        //calling method implementation paging
        //調用方法時，底層封裝，把分頁所有數據封裝到 pageTeacher 對象裏面
        teacherService.page(pageTeacher,null);

        //total records
        long total = pageTeacher.getTotal();

        //data list set
        List<EduTeacher> records = pageTeacher.getRecords();

        //第一種方法
        /*Map map = new HashMap();
        map.put("total",total);
        map.put("rows",records);
        return R.ok().data(map);*/

        //第二種方法
        return R.ok().data("total",total).data("rows",records);

    }

    //4 condition query and paging query
    @ApiOperation(value = "條件分頁查詢")
    @PostMapping("pagingTeacherCondition/{current}/{limit}")
    public R pagingTeacherCondition(@PathVariable long current, @PathVariable long limit,
                                    @RequestBody(required = false) TeacherQuery teacherQuery){
        //create page Object
        Page<EduTeacher> pageTeacher = new Page<>(current, limit);
        //構建條件
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        //多條件組合查詢，判斷條件是否為空，不爲空則拼接條件
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        if (!StringUtils.isEmpty(name)){
            //構建條件
            wrapper.like("name",name);
        }
        if (!StringUtils.isEmpty(level)) {
            wrapper.eq("level",level);
        }
        if (!StringUtils.isEmpty(begin)) {
            wrapper.ge("gmt_create",begin);
        }
        if (!StringUtils.isEmpty(end)) {
            wrapper.le("gmt_create",end);
        }
        //sort
        wrapper.orderByDesc("gmt_create");

        //調用方法實現查詢分頁帶條件查詢
        teacherService.page(pageTeacher,wrapper);

        //total records
        long total = pageTeacher.getTotal();

        //data list set
        List<EduTeacher> records = pageTeacher.getRecords();

        return R.ok().data("total",total).data("rows",records);
    }

    //5 add teacher interface method
    @ApiOperation(value = "插入老師記錄")
    @PostMapping("addTeacher")
    public R addTeacher(@RequestBody EduTeacher eduTeacher){
        boolean save = teacherService.save(eduTeacher);
        if (save) {
            return R.ok();
        }else {
            return R.error();
        }
    }

    //6 by teacherId query
    @ApiOperation(value = "根據老師ID查詢")
    @GetMapping("getTeacher/{id}")
    public R getTeacher(@PathVariable String id){
        EduTeacher teacher = teacherService.getById(id);
        return R.ok().data("teacher",teacher);
    }

    //7 update teacher
    @ApiOperation(value = "更新老師表記錄")
    @PostMapping("udpateTeacher")
    public R updateTeacher(@RequestBody EduTeacher eduTeacher){
        boolean flag = teacherService.updateById(eduTeacher);
        if (flag) {
            return R.ok();
        }else{
            return R.error();
        }
    }

}

